package com.suncaper.han_yi_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suncaper.han_yi_system.pojo.dto.BatchQueryDto;
import com.suncaper.han_yi_system.pojo.entity.Batch;
import com.suncaper.han_yi_system.result.Result;
import com.suncaper.han_yi_system.service.BatchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/batch")
@CrossOrigin(origins = "*")
@Tag(name = "批次管理")
@Slf4j
public class BatchController {

    @Autowired
    BatchService batchService;

    @Operation(summary = "新增批次信息")
    @PostMapping("/save")
    public Result save (@RequestBody Batch batch) {
        batch.setCreateTime(LocalDateTime.now());
        batchService.save(batch);
        return Result.ok();
    }

    @Operation(summary = "新增衣服类型")
    @PostMapping("/saveClothingStyle/{batchId}")
    public Result saveClothingStyle(@RequestBody List<Long> ids, @PathVariable Long batchId){
        batchService.saveClothingStyle(ids, batchId);
        return Result.ok();
    }

    @Operation(summary = "删除衣服类型")
    @PostMapping("/deleteClothingStyle/{batchId}")
    public Result deleteClothingStyle(@RequestBody List<Long> ids, @PathVariable Long batchId){
        batchService.deleteClothingStyle(batchId, ids);
        return Result.ok();
    }

    @Operation(summary = "更新批次信息")
    @PutMapping("/update")
    public Result update(@RequestBody Batch batch) {
        batch.setUpdateTime(LocalDateTime.now());
        batchService.updateById(batch);
        return Result.ok();
    }

    @Operation(summary = "删除批次信息")
    @DeleteMapping("/delete/{id}")
    public Result deleteBatch(@PathVariable Long id) {
        if (Objects.isNull(id)) {
            return Result.error().message("id不能为空");
        }
        batchService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id获取批次信息")
    @GetMapping("/getById/{id}")
    public Result getBatchById(@PathVariable Long id) {
        if (Objects.isNull(id)) {
            return Result.error().message("id不能为空");
        }
        return Result.ok().data("batch", batchService.getById(id));
    }

    @Operation(summary = "获取批次列表")
    @RequestMapping("/list")
    public Result getBatchList(@RequestBody BatchQueryDto batchQueryDto){
        QueryWrapper<Batch> queryWrapper = new QueryWrapper<>();
        if(!Objects.isNull(batchQueryDto.getAcademicYear())) {
            queryWrapper.eq("academic_year", batchQueryDto.getAcademicYear());
        }
        if(!Objects.isNull(batchQueryDto.getDifficultyLevel())) {
            queryWrapper.eq("difficulty_level", batchQueryDto.getDifficultyLevel());
        }
        queryWrapper.orderByDesc("academic_year");
        Page<Batch> page = new Page<>(batchQueryDto.getPage(), batchQueryDto.getLimit());
        Page<Batch> list = batchService.page(page, queryWrapper);
        return Result.ok().data("records",list.getRecords()).data("total",list.getTotal());
    }

    @Operation(summary = "获取我的批次信息")
    @GetMapping("/getMyBatch")
    public Result getMyBatch(){

        Batch batch = batchService.findMyBatch();

        return Result.ok().data("batch", batch);
    }

}